package com.calm.editor.service;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.calm.editor.api.TemplateService;
import com.calm.editor.entity.Template;
import com.calm.editor.entity.TemplateKey;
import com.calm.editor.servlet.Event;

@Service("template")
@Transactional
public class TemplateServiceImpl extends BaseServiceImpl<Template,TemplateKey> implements TemplateService {
	@Event("loadTemplate")
	public Template getById(TemplateKey id) {
		return super.getById(id);
	}
	@Event("listByNamespace")
	public List<Template> listByNamespace(final String id) {
		List<Template> list=query(new Query<Template>() {

			@Override
			public void query(CriteriaBuilder builder,
					CriteriaQuery<Template> query, Root<Template> from) {
				Path<TemplateKey> path = from.get("id");
				builder.equal(path.get("namespace"), id);
			}
		});
		return list;
	}
	@Override
	public Class<Template> getEntityClass() {
		return Template.class;
	}
}
